﻿using MySql.Data.MySqlClient;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserManage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static SqlSugarClient DB
        {
            get
            {
                if (db == null)
                {
                    string connectionString = "server=127.0.0.1;database=mini_standard_pc;uid=root;pwd=root;port = 3306;Persist Security Info=True;Charset=utf8;Allow User Variables=True;";
                    db = new SqlSugarClient(
                     new ConnectionConfig()
                     {
                         ConnectionString = connectionString,
                         DbType = SqlSugar.DbType.MySql,//设置数据库类型mysq
                         IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                         InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                     });
                }
                return db;
            }
        }

        private static SqlSugarClient db;


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitialDB();
        }

        public static void InitialDB()
        {
            try
            {
                MySqlConnectionStringBuilder connectionString = new MySqlConnectionStringBuilder();
                connectionString.ConnectionString = "server=127.0.0.1;database=mini_standard_pc;uid=root;pwd=root;port = 3306;Persist Security Info=True;Charset=utf8;Allow User Variables=True;";

                MySqlConnection conn = new MySqlConnection(connectionString.ConnectionString);//
                string sqlDB = $"SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='{connectionString.Database}';";
                MySqlDataAdapter adp = new MySqlDataAdapter(sqlDB, conn);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MySqlScript script = new MySqlScript(new MySqlConnection(connectionString.ConnectionString), File.ReadAllText(@"Sqlscript\20230508110000_InitialDB.sql"));
                    script.Delimiter = "$$";
                    script.Execute();
                }
                //DirectoryInfo exePath = new DirectoryInfo(Environment.CurrentDirectory);
                //string pathFullName = exePath.Parent.Parent.FullName;//项目所在目录，即项目最外层目录
                //LogInDB.Debug($"更新数据库失败{$"{pathFullName}\\ATL_MES\\Models\\DBEntity"}");
                //DB.DbFirst.IsCreateAttribute().CreateClassFile(exePath.FullName, "PTF.Models.DBEntity");//文件地址，指定实体命名空间

                Type[] types = Assembly.GetExecutingAssembly()//如果 .dll报错，可以换成 xxx.exe 有些生成的是exe 
                 .GetTypes().Where(it => it.FullName.Contains("PTF.Models.DBEntity"))//命名空间过滤，当然你也可以写其他条件过滤
                .ToArray();//断点调试一下是不是需要的Type，不是需要的在进行过滤
                           //同步数据表结构
                db.CodeFirst.SetStringDefaultLength(1024).InitTables(types);//多表同步

            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
